<?php

/**
 * 结算投放计划，每分钟检查到结算时间的计划
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bizAd\AdPlan as mAdPlan;
use Biz\service\bizAd\AdPlan;

for ($i = 0; $i < 60; $i++) {
    //每分钟执行1次，执行60分钟，60次
    var_dump('执行第' . ($i + 1) . '次');
    main();
    sleep(60);
}

function main()
{
    $now = time();
    $where = [
        'settle_time[<=]' => $now,
        'settle_status' => mAdPlan::SETTLE_STATUS_NORMAL,
        'plan_status' => mAdPlan::PLAN_STATUS_OPEN,
    ];
    $list = mAdPlan::getPlanList($where);
    foreach ($list as $row) {
        $res = AdPlan::settlePlan($row);
        var_dump('结算投放计划:' . ($res ? '成功' : '失败!') . '#plan_id:' . $row['id']);
    }
}